﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LMKJ.Classes;
using System.Text.RegularExpressions;

namespace LMKJ
{
    public partial class addresident : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dobInit();
            }
        }

        protected void dobInit()
        {
            int thisYear = DateTime.Today.Year;

            for (int m = 1; m <= 12; m++)
            {
                monthDdl.Items.Add(m.ToString());
            }

            for (int y = thisYear - 51; y >= thisYear - 100; y--)
            {
                yearDdl.Items.Add(y.ToString());
            }
        }

        protected void monthDdl_SelectedIndexChanged(object sender, EventArgs e)
        {
            int monthNo;
            int dayNo = 0;

            if (monthDdl.SelectedItem.Text != "Month")
            {
                dayDdl.Enabled = true;
                yearDdl.Enabled = true;

                if (dayDdl.SelectedIndex > 1)
                {
                    dayNo = Convert.ToInt32(dayDdl.SelectedItem.Text);
                }

                dayDdl.Items.Clear();
                dayDdl.Items.Add("Day");

                monthNo = monthDdl.SelectedIndex;

                for (int d = 1; d <= 31; d++)
                {
                    dayDdl.Items.Add(d.ToString());
                    if (monthNo == 2 && d == 29)
                    {
                        break;
                    }
                    else if ((monthNo == 4 || monthNo == 6 || monthNo == 9 || monthNo == 11) && d == 30)
                    {
                        break;
                    }
                }

                if (dayDdl.Items.Count - 1 >= dayNo)
                {
                    dayDdl.SelectedIndex = dayNo;
                }
            }
            else if (monthDdl.SelectedItem.Text == "Month")
            {
                dayDdl.SelectedIndex = 0;
                yearDdl.SelectedIndex = 0;
                dayDdl.Enabled = false;
                yearDdl.Enabled = false;
            }
        }

        protected void addBtn_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                Resident r = new Resident();
                r.Nric = nricTbx.Text;
                r.Name = nameTbx.Text;
                r.Dob = yearDdl.SelectedItem.Text + "-" + monthDdl.SelectedItem.Text + "-" + dayDdl.SelectedItem.Text;
                r.Gender = genderRbl.Text;
                r.Address = addressTbx.Text;
                r.Postal = postalTbx.Text;
                r.Ethnicity = ethnicityDdl.SelectedItem.Text;
                r.Description = descriptionTbx.Text;

                int success = DatabaseManager.CreateResident(r);

                if (success == 1)
                {
                    string jsPopup = "<script type='text/javascript'>" +
                        "alert('Successfully Created Resident!');" +
                        "window.location.href = 'searchresidents.aspx';" +
                        "</script>";
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", jsPopup);
                }
            }
        }

        protected void cancelBtn_Click(object sender, EventArgs e)
        {
            Response.Redirect("searchresidents.aspx");
        }

        protected void fieldsValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            if (String.IsNullOrEmpty(nricTbx.Text.Trim())
                || String.IsNullOrEmpty(nameTbx.Text.Trim())
                || String.IsNullOrEmpty(addressTbx.Text.Trim())
                || String.IsNullOrEmpty(postalTbx.Text.Trim())
                || monthDdl.SelectedIndex == 0 || dayDdl.SelectedIndex == 0 || yearDdl.SelectedIndex == 0)
            {
                args.IsValid = false;
            }
            else
            {
                args.IsValid = true;
            }
        }

        protected void residentValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            bool residentValid = DatabaseManager.CheckResidentValidity(nricTbx.Text.Trim());

            if (residentValid)
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }

        protected void postalValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            Regex NumberOnly = new Regex("\\d+$");

            if (NumberOnly.IsMatch(postalTbx.Text))
            {
                args.IsValid = true;
            }
            else
            {
                args.IsValid = false;
            }
        }
    }
}